//
//  FLLFiveItemsTableViewCell.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/30.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLFiveItemsTableViewCell.h"
#import "FLLFiveItemsCollectionViewCell.h"
#import "FLLMacro.h"
#import "UIImageView+WebCache.h"
#import "FLLSceneInfoViewController.h"


@interface FLLFiveItemsTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *nameArray;
@property (nonatomic, strong) NSMutableArray *idArray;
@property (nonatomic, strong) NSMutableArray *videoArray;
@end

@implementation FLLFiveItemsTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    self.fiveItemsCollectionView.delegate = self;
    self.fiveItemsCollectionView.dataSource = self;
    self.fiveItemsCollectionView.backgroundColor = [UIColor whiteColor];
    self.fiveItemsLayout.minimumInteritemSpacing = CellMargin;
    self.fiveItemsLayout.minimumLineSpacing = CellMargin;
    self.fiveItemsLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.fiveItemsLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    [_fiveItemsCollectionView registerNib:[UINib nibWithNibName:@"FLLFiveItemsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"fiveItemsCollectionCell"];
    
    
    
}
#pragma mark -- DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_fiveItemArray.count != 0) {
        return 5;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FLLFiveItemsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fiveItemsCollectionCell" forIndexPath:indexPath];
    
    
    // 赋值
    // 图片
    NSMutableArray *picArray = [NSMutableArray array];
    for (NSDictionary *dic in _fiveItemArray) {
        if ([[dic valueForKey:@"type"] isEqualToString:@"image"]) {
            NSMutableDictionary *picDic = [NSMutableDictionary dictionary];
            [picDic setValue:[dic valueForKey:@"content"] forKey:@"image"];
            [picArray addObject:picDic];
        }
    }
    [cell.backgroundPicImageView sd_setImageWithURL:[NSURL URLWithString:picArray[indexPath.row][@"image"]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    // 文字
    self.nameArray = [NSMutableArray array];
    for (NSDictionary *dic in _fiveItemArray) {
        if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && ([[dic valueForKey:@"id_Data"] integerValue] == 2 || [[dic valueForKey:@"id_Data"] integerValue] == 3)) {
            NSMutableDictionary *picDic = [NSMutableDictionary dictionary];
            [picDic setValue:[dic valueForKey:@"content"] forKey:@"name"];
            [_nameArray addObject:picDic];
        }
    }
    if (indexPath.row == 0) {
        cell.itemDishNameLabel.text = _nameArray[0][@"name"];
        cell.dishCountLabel.text = _nameArray[1][@"name"];
        cell.playVideo.image = [UIImage new];
    } else {
        cell.itemDishNameLabel.text = nil;
        cell.dishCountLabel.text = nil;
        cell.playVideo.image = [UIImage imageNamed:@"love_Play_Icon31-1"];
    }
    
    
    
    // 传到下一页的id
    self.idArray = [NSMutableArray array];
    for (NSDictionary *dic in _fiveItemArray) {
        if ([[dic valueForKey:@"type"] isEqualToString:@"image"]) {
            NSMutableDictionary *idDic = [NSMutableDictionary dictionary];
            [idDic setValue:[dic valueForKey:@"link"] forKey:@"link"];
            [_idArray addObject:idDic];
        }
    }
    
    // 视频 做下一页再做
    
    
    
    
        return cell;
    
}
// 给item确定大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat itemWidth = (_fiveItemsCollectionView.bounds.size.width - 2 * CellMargin) / 3.0;
    CGFloat itemHeight = _fiveItemsCollectionView.bounds.size.height;
    if (indexPath.row == 0) {
        return CGSizeMake(itemWidth, itemHeight);
    } else {
        return CGSizeMake(itemWidth , (itemHeight -  CellMargin) / 2.0);
        
    }
}
- (void)setFiveItemArray:(NSArray<Widget_Data *> *)fiveItemArray
{

    _fiveItemArray = fiveItemArray;
    [self.fiveItemsCollectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
        self.sceneBlock(_idString);
    } else {
    
        self.foodBlock(_idArray[indexPath.row][@"link"]);
    }
    
}
- (void)setIdString:(NSString *)idString
{

    _idString = idString;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
